package cn.tedu.eguser.controler;

import cn.tedu.eguser.EguserApplication;
import cn.tedu.eguser.base.config.response.JsonResult;
import cn.tedu.eguser.base.config.response.StatusCode;
import cn.tedu.eguser.mapper.UserMapper;
import cn.tedu.eguser.pojo.dto.UserAddParam;
import cn.tedu.eguser.pojo.dto.UserUpdateParam;
import cn.tedu.eguser.pojo.entity.User;
import cn.tedu.eguser.pojo.vo.UserListVO;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Slf4j
@Validated
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private EguserApplication eguserApplication;

    @PostMapping("add")
    public JsonResult addUser(@RequestBody UserAddParam userAddParam){
        System.out.println("添加用户："+userAddParam);
        User user = new User();
        BeanUtils.copyProperties(userAddParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        System.out.println("添加数据库:"+user);
        userMapper.insertUser(user);
        return  JsonResult.ok();
    }
    @GetMapping("list")
    public JsonResult listAllUser() {
        return JsonResult.ok(userMapper.listAllUser());
    }

@GetMapping
        public JsonResult delUser(Long id) {
    System.out.println("id = " + id);
    int i = userMapper.deleteUserById(id);
    if (i > 0) {
        return JsonResult.ok();
    }
    return new JsonResult(StatusCode.OPERATTION_FAILED);
}

        /**
         * 4.用户修改功能
         */

        @PostMapping("update")
        public JsonResult updateUser(UserUpdateParam userUpdateParam){
            System.out.println("userUpdateParam: " + userUpdateParam);
            User user = new User();
            BeanUtils.copyProperties(userUpdateParam, user);
            user.setUpdateTime(new Date());
            System.out.println("准备修改数据库: " + user);
            int i = userMapper.updateUserById(user);
            if (i > 0){
                return JsonResult.ok("修改用户成功");
            }
            return JsonResult.ok("修改用户失败");
        }
    }

